
/**
 * time工具类
 * @author liangyongle
 * @date 2018/8/20 0020
 */

package com.partners.utils;

import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.TimeZone;

/**
 * 日期工具类
 */
@Slf4j
public class TimestampUtils {

    /**
     * 根据localDateTime获取时间戳
     * @param localDateTime LocalDateTime
     * @return 时间戳
     */
    public static Integer timestamp(LocalDateTime localDateTime) {
        Long timestamp = localDateTime.toEpochSecond(ZoneOffset.of("+8"));
        return timestamp.intValue();
    }

    /**
     * 获取当前的时间戳
     * @return 时间戳
     */
    public static Integer timestamp() {
        LocalDateTime now = LocalDateTime.now();
        Long currentTimeStamp = now.toEpochSecond(ZoneOffset.of("+8"));
        return currentTimeStamp.intValue();
    }

    /**
     * 测试时间戳转时间
    * */

    public static String getDateTimeTimesTamps(Integer timestamp){
        Long time = timestamp.longValue()*1000L;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = df.format(Long.valueOf(time));
        return format;
    }

    /**
     * 获取当前分钟的时间戳，即秒为0
     * @return 时间戳
     */
    public static Integer timestampIgnoreSecond() {
        return timestamp() - LocalDateTime.now().getSecond();
    }

    /**
     * *
     * 自定义时间戳
    * */

    public static String  getDateTimeOfTimestamp(Integer timestamp,String dateFormat) {
        Long time = timestamp.longValue()*1000L;
//        TimeZone tz = TimeZone.getTimeZone("ETC/GMT-8");
//        TimeZone.setDefault(tz);
        SimpleDateFormat df = new SimpleDateFormat(dateFormat);
        String format = df.format(Long.valueOf(time));
        return format;
    }

    /*
    * 获取当前时间
    * */
    public static String getSimpleDateFormat(String formats){
        SimpleDateFormat format = new SimpleDateFormat(formats);
        Date nowTime = new Date(System.currentTimeMillis());
        return  format.format(nowTime);
    }

}
